package com.firebird.app.fekpsl.ui.activity.classroom

import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.adapter.classroom.QuestionPracticeAdapter
import com.firebird.app.fekpsl.base.BaseMvpSwipeBackActivity
import com.firebird.app.fekpsl.mvp.classroom.QuestionPracticeContract
import com.firebird.app.fekpsl.mvp.classroom.QuestionPracticePresenter
import com.firebird.app.fekpsl.bean.BaseListResponseBody
import com.firebird.app.fekpsl.bean.Paper
import com.firebird.app.fekpsl.widget.SpaceItemDecoration
import kotlinx.android.synthetic.main.fragment_common_refresh_layout.*

class QuestionPracticeActivity : BaseMvpSwipeBackActivity<QuestionPracticeContract.View, QuestionPracticeContract.Presenter>(), QuestionPracticeContract.View {
    /**
     * 每页数据的个数
     */
    private var pageSize = 10;

    /**
     * datas
     */
    private val datas = mutableListOf<Paper>()
    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        SpaceItemDecoration(this)
    }

    private val questionPracticeAdapter: QuestionPracticeAdapter by lazy {
        QuestionPracticeAdapter(this@QuestionPracticeActivity,datas)
    }

    /**
     * is Refresh
     */
    private var isRefresh = true

    override fun createPresenter(): QuestionPracticeContract.Presenter = QuestionPracticePresenter()

    override fun attachLayoutRes(): Int = R.layout.classroom_question_practice

    override fun showLoading() {
        // swipeRefreshLayout.isRefreshing = isRefresh
    }

    override fun hideLoading() {
        swipeRefreshLayout?.isRefreshing = false
        if (isRefresh) {
            questionPracticeAdapter.setEnableLoadMore(true)
        }
    }

    override fun showError(errorMsg: String) {
        super.showError(errorMsg)
        if (isRefresh) {
            questionPracticeAdapter.setEnableLoadMore(true)
        } else {
            questionPracticeAdapter.loadMoreFail()
        }
    }

    override fun initData() {
    }

    override fun initView() {
        super.initView()
        /*
        toolbar.run {
            title = getString(R.string.question_practice)
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }*/
        swipeRefreshLayout.run {
            setOnRefreshListener(onRefreshListener)
        }
        recyclerView.run {
            layoutManager = LinearLayoutManager(this@QuestionPracticeActivity)
            adapter = questionPracticeAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(recyclerViewItemDecoration)
        }
        questionPracticeAdapter.run {
            bindToRecyclerView(recyclerView)
            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            onItemClickListener = this@QuestionPracticeActivity.onItemClickListener
        }
    }

    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        if (datas.size != 0) {
            val data = datas[position]
            val practice_subject_id=data.subject_id
            val practice_question_total_num=data.question_count
            val practice_title=data.name
            QuestionPracticeMarkActivity.start(this@QuestionPracticeActivity,
                    practice_subject_id, practice_question_total_num,practice_title)
        }
    }

    override fun start() {
        mPresenter?.getAllPaperList(0)
    }

    override fun showAllPaperList(body: BaseListResponseBody<Paper>) {
        body.datas.let {
            questionPracticeAdapter.run {
                if (isRefresh) {
                    replaceData(it)
                } else {
                    addData(it)
                }
                pageSize = body.size
                if (body.over) {
                    loadMoreEnd(isRefresh)
                } else {
                    loadMoreComplete()
                }
            }
        }
    }

    /**
     * RefreshListener
     */
    private val onRefreshListener = SwipeRefreshLayout.OnRefreshListener {
        isRefresh = true
        questionPracticeAdapter.setEnableLoadMore(false)
        mPresenter?.getAllPaperList(0)
    }
    /**
     * LoadMoreListener
     */
    private val onRequestLoadMoreListener = BaseQuickAdapter.RequestLoadMoreListener {
        isRefresh = false
        swipeRefreshLayout.isRefreshing = false
        val page = questionPracticeAdapter.data.size / pageSize + 1
        mPresenter?.getAllPaperList(page)
    }

}
